package br.usp.pcs.acs.negocios.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.usp.pcs.acs.negocios.dao.interfaces.PedidoDaoInterface;
import br.usp.pcs.acs.negocios.dto.PedidoDto;


public class PedidoDao implements PedidoDaoInterface
{   
	public ArrayList<PedidoDto> findPendentes() {
		String busca =  "SELECT * FROM pedido WHERE autorizado < 8";
		return finding(busca);
}


	@Override
	public void edit(PedidoDto inDto)
	{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_U);
			
			pstmt.setInt(6, inDto.getId());//id
			pstmt.setInt(1,inDto.getCa_id());
			pstmt.setInt(2,inDto.getGa_id());
			pstmt.setInt(3,inDto.getQtd());
			pstmt.setString(4,inDto.getUnidade());
			pstmt.setByte(5, inDto.getAutorizado());
			
			pstmt.executeUpdate();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}

	@Override
	public ArrayList<PedidoDto> findAll() {
		return finding(CRUD_R);
	}
	private ArrayList<PedidoDto> finding(String busca) {
		ArrayList<PedidoDto> LoutDto = new ArrayList<PedidoDto>();
		PedidoDto tmpDto = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(busca);			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				tmpDto = new PedidoDto();
				tmpDto.setId(rs.getInt(1));
				tmpDto.setCa_id(rs.getInt(2));
				tmpDto.setGa_id(rs.getInt(3));
				tmpDto.setQtd(rs.getInt(4));
				tmpDto.setUnidade(rs.getString(5));
				tmpDto.setAutorizado(rs.getByte(6));
				
				LoutDto.add(tmpDto);
			}
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
		
		return LoutDto;
	}

	@Override
	public PedidoDto findById(int id) {
		String busca =  "SELECT * FROM pedido WHERE id = " + id;
		ArrayList<PedidoDto> ret= finding(busca);
		return ret.isEmpty()?null:ret.get(0);
	}

	@Override
	public void insert(PedidoDto inDto) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		
		try{
			conn = DatabaseConnect.getConnection();
			
			pstmt = conn.prepareStatement(CRUD_C);
			
			pstmt.setNull(1, java.sql.Types.INTEGER);//id
			pstmt.setInt(2,inDto.getCa_id());
			pstmt.setInt(3,inDto.getGa_id());
			pstmt.setInt(4,inDto.getQtd());
			pstmt.setString(5,inDto.getUnidade());
			pstmt.setByte(6, inDto.getAutorizado());
			
			pstmt.execute();
		}
		
		
		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (rs != null){
		    	try {rs.close();}catch (SQLException sqlEx) { } // ignore 
		    	rs = null;
		    }
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}

	@Override
	public void remove(int id) 
	{
		Connection conn = null;
		PreparedStatement pstmt = null;		
		try{
			conn = DatabaseConnect.getConnection();			
			pstmt = conn.prepareStatement(CRUD_D);			
			pstmt.setInt(1, id);			
			pstmt.execute();
		}		
		catch (SQLException ex){
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
		finally {
		    if (pstmt != null){
		        try {pstmt.close();}catch (SQLException sqlEx) { } // ignore
		        pstmt = null;
		    }
		    if (conn != null){
		    	try{conn.close();}catch (SQLException sqlEx) { } // ignore
		    }
		}
	}
	

}
